import axios from 'axios';
import env from '../../build/env';

let util = {
	baseURL: env === 'development' ? '' : ''
};

util.title = function (title) {
    title = title ? 'Beeun - ' + title : 'Beeun';
    window.document.title = title;
};

util.ajax = axios.create({
    baseURL: util.baseURL,
    params: {},
    headers: {
        // Timestamp : parseInt(new Date().getTime() / 1000)
    },
    timeout: 30000
});

// 请求拦截器
util.ajax.interceptors.request.use(
    config => {
        config.params['token'] = localStorage.token;
        return config;
    }
);

// 响应拦截器
util.ajax.interceptors.response.use(
    response => {
        if(response.data.code !== 200) {
            return Promise.reject();
        }
        return response.data.result;
    },
    error => {
        return Promise.reject();
    }
);

export default util;
